
const path = require("path")
const webpack = require("webpack")
const HtmlWebpackPlugin = require("html-webpack-plugin")
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
const jquery = require("jquery")

const pages = [
    {
        pageName:  "index",
        chunks: "index",
        title: "首页"
    },
    {
        pageName:  "about",
        chunks: "about",
        title: "关于我们"
    }
]

module.exports = {

    entry: {
        index: "./src/pages/index",
        about: "./src/pages/about"
    },

    output: {
        filename: "./js/[name].js",
        path: path.resolve(__dirname, "dist")
    },

    mode: "development",

    module: {
        rules: [
            {
                test: /\.css/,
                use: [
                    //"style-loader",
                    MiniCssExtractPlugin.loader,
                    "css-loader"
                ]
            }
        ]
    },

    plugins: [

        new MiniCssExtractPlugin({
            filename: "css/[name].css",
            chunkFilename: "[name].css"
        }),

        ...pages.map(pageItem => {
            return new HtmlWebpackPlugin({
                filename: `${pageItem.pageName}.html`,
                template: `./src/pages/${pageItem.pageName}/index.html`,
                chunks: [`${pageItem.chunks}`],
                hash: true,
                title: `${pageItem.title}`,
                // minify: {
                //     collapseWhitespace: true
                // }
            })
        }),
        new CleanWebpackPlugin(),

        new webpack.ProvidePlugin({
            jQuery: jquery
        })
    ],

    optimization: {
        splitChunks: {
            cacheGroups: {
                common: {
                    name: "common",
                    chunks: "initial",
                    minSize: 1,
                    priority: 1,
                    minChunks: 2
                },
                vendors: {
                    name: "vendors",
                    test: /(node_modules)/,
                    chunks: "all",
                    priority: 10
                }
            }
        }
    }

}
